{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "89a25d3f",
   "metadata": {},
   "source": [
    "## ReAct Agent\n",
    "\n",
    "\n",
    "ReAct(reason, act)整合了推理和行动能力，是一种使用自然语言推理解决复杂任务的语言模型。ReAct旨在用于允许LLM执行某些操作的任务。例如，在MRKL系统中，LLM可以与外部API交互以检索信息。当提出问题时，LLM可以选择执行操作以检索信息，然后根据检索到的信息回答问题。\n",
    "\n",
    "ReAct系统可以被视为具有推理和行动能力的MRKL系统。\n",
    "给机器人小明一个任务：去厨房为你做一杯咖啡。小明不仅要完成这个任务，还要告诉你他是如何一步步完成的。\n",
    "\n",
    "### 缺乏ReAct的机器人系统：\n",
    "\n",
    "1. 小明直接跑到厨房。\n",
    "2. 你听到了一些声音，但不知道小明在做什么。\n",
    "3. 过了一会儿，小明回来给你一杯咖啡。\n",
    "4. 这样的问题是，你不知道小明是怎么做咖啡的，他是否加了糖或奶，或者他是否在过程中遇到了任何问题。\n",
    "\n",
    "### ReAct的机器人系统：\n",
    "\n",
    "- Thought 1：“我现在去厨房。”\n",
    "- Act 1：“启动进入厨房。”\n",
    "- Obs 1：“看到厨房内的环境。”\n",
    "\n",
    "- Thought 2：“我需要找到咖啡粉和咖啡机。”\n",
    "- Act 2：“寻找咖啡粉和咖啡机。”\n",
    "- Obs 2：“找到了咖啡粉和咖啡机。”\n",
    "\n",
    "- Thought 3：“我现在可以开始煮咖啡了。”\n",
    "- Act 3：“开始煮咖啡。”\n",
    "- Obs 3：“咖啡做好了。”\n",
    "\n",
    "- Thought 4：“可以把咖啡送过去了。”\n",
    "- Act 4：“送咖啡。”\n",
    "- Obs 4：“得到了表扬。”\n",
    "\n",
    "ReAct能够首先通过推理问题（Thought 1），然后执行一个动作（Act 1）。然后它收到了一个观察（Obs 1），并继续进行这个思想，行动，观察循环，直到达到结论。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ccc27dd",
   "metadata": {},
   "source": [
    "```python\n",
    "PromptTemplate(\n",
    "    input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools'],\n",
    "    template='Answer the following questions as best you can. You have access to the following tools:\\n\\n{tools}\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [{tool_names}]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: {input}\\nThought:{agent_scratchpad}'\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aec9f7f2",
   "metadata": {},
   "source": [
    "## 提示词\n",
    "\n",
    "```\n",
    "Answer the following questions as best you can. You have access to the following tools:\n",
    "\n",
    "{tools}\n",
    "\n",
    "Use the following format:\n",
    "\n",
    "Question: the input question you must answer\n",
    "Thought: you should always think about what to do\n",
    "Action: the action to take, should be one of [{tool_names}]\n",
    "Action Input: the input to the action\n",
    "Observation: the result of the action\n",
    "... (this Thought/Action/Action Input/Observation can repeat N times)\n",
    "Thought: I now know the final answer\n",
    "Final Answer: the final answer to the original input question\n",
    "\n",
    "Begin!\n",
    "\n",
    "Question: {input}\n",
    "Thought:{agent_scratchpad}\n",
    "```\n",
    "\n",
    "\n",
    "```\n",
    "尽可能回答以下问题。 您可以使用以下工具：\n",
    "\n",
    "{tools}\n",
    "\n",
    "使用以下格式：\n",
    "\n",
    "Question：您必须回答的输入问题\n",
    "Thought：你应该时刻思考该做什么\n",
    "Action：要采取的操作，应该是 [{tool_names}] 之一\n",
    "Action Input：动作的输入\n",
    "Observation：行动的结果\n",
    "...（这个想法/行动/行动输入/观察可以重复N次）\n",
    "想法：我现在知道了最终答案\n",
    "Final Answer：原始输入问题的最终答案\n",
    "\n",
    "开始！\n",
    "\n",
    "问题：{input}\n",
    "想法：{agent_scratchpad}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1636037f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
